<script setup lang="ts">
//
import { onLoad } from '@dcloudio/uni-app';
import CustomNavbar from './components/CustomNavbar.vue';
import CategoryPanel from './components/CategoryPanel.vue';
import HotPanel from './components/HotPanel.vue';
import {getHomeBannerAPI, getHomeCategoryAPI, getHotDataAPI} from "@/services/home"
import {ref} from "vue"
import type {BannerItem, CategoryItem, HotItem} from "@/types/home"
import PageSkeleton from "./components/PageSkeleton.vue"
import { useGuessList } from '@/composables';

const bannerList = ref<BannerItem[]>([]);
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI();

  if(res.code == '1') {
    bannerList.value = res.result
  }
}

// 获取前台分类数据
const categoryList = ref<CategoryItem[]>([]);
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI();

  if(res.code == '1') {
    categoryList.value = res.result
  }
}

// 获取前台分类推荐数据
const hotList = ref<HotItem[]>([]);
const getHotData = async () => {
  const res = await getHotDataAPI()

  if(res.code == '1') {
    hotList.value = res.result
  }
}

const isLoading = ref(false);

onLoad(async () => {
  isLoading.value = true;
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHotData()])
  isLoading.value = false;
})

const {guessRef, onScrolltolower} = useGuessList();

const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  isTriggered.value = true;
  guessRef.value?.resetData();
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHotData(), guessRef.value?.getMore()]);
  isTriggered.value = false;
}
</script>

<template>  
  <!-- 自定义导航栏 -->
  <CustomNavbar />
  <scroll-view 
    refresher-enabled 
    @refresherrefresh="onRefresherrefresh" 
    @scrolltolower="onScrolltolower" 
    scroll-y 
    :refresher-triggered="isTriggered"
    class="scoll-view">

    <PageSkeleton v-if="isLoading" />

    <block v-else>
      <!-- 自定轮播图 -->
      <XtxSwiper :list="bannerList" />
      <!-- 分类面板 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :list="hotList" />
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef" />
    </block>

  </scroll-view>


</template>

<style lang="scss">
//
page {
  background-color: #f7f7f7;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.scoll-view {
  flex: 1;
}
</style>
